package com.cq.androidtools.reflex

import java.lang.reflect.Constructor

/**
 * 作者: CQ
 * 日期: 2021-03-26
 * 说明:
 */

public fun main(arg: Array<String>) {
//    huh Class 对象的 newInstance() 方法
    val clz: Class<*> = ReflexBean::class.java
//    通过 Constructor 对象创建类对象可以选择特定构造方法
    val constructor = clz.getConstructor(String::class.java)
    val apple = constructor.newInstance("sss") as ReflexBean

    println("---------------------获取构造函数并使用")
    println(apple.toString())



    println("---------------------获取方法并调用")
    val mothods = clz.getMethod("show")
    mothods.invoke(apple)

    val mothods1 = clz.getMethod("shows", String::class.java)
    mothods1.invoke(apple, "ssssssss")
}


class ReflexBean {
    lateinit var name: String
    var age: Int = 0

    constructor() {

    }

    constructor(name: String) {
        this.name = name
    }

    constructor(name: String, age: Int) {
        this.name = name
        this.age = age
    }

    fun show() {
        println("show")
    }

    fun shows(x: String) {
        println("show :  $x")
    }

    override fun toString(): String {
        return "ReflexBean(name='$name', age=$age)"
    }


}